package org.go.examples.example_08;

import org.go.examples.example_01.MyTask;
import org.go.scheduler.Scheduler;
import org.go.scheduler.StdSchedulerFactory;
import org.go.trigger.CronScheduleBuilder;
import org.go.trigger.Trigger;
import org.go.trigger.calendar.DailyCalendar;
import org.go.work.Work;
import org.go.work.WorkBuilder;

public class Main {
	/**/
	public void execDailyCalendar() throws Exception {
		String beginTime = "18:20:00";//start
		String endTime = "18:22:00";//end
		String cron = "*/5 * * * * ?"; //once every 5 seconds.
		//定义DailyCalendar
		DailyCalendar dailyCal = new DailyCalendar(beginTime, endTime);
		dailyCal.setInvertTimeRange(true);

		// Creates the scheduler.
		Scheduler scheduler = new StdSchedulerFactory().getScheduler("DefaultScheduler");

		//
		Trigger cronTrigger = CronScheduleBuilder.cronSchedule(cron).build();
		cronTrigger.setCalendarName("calName");
		//
		//添加calendar到trigger
		scheduler.addCalendar("calName", dailyCal, true, false);
		//
		Work work = WorkBuilder.newWork(MyTask.class).withIdentity("job1", "group1").build();
		scheduler.scheduler(work, cronTrigger);
	}

	public static void main(String[] args) throws Exception {
		try {
			new Main().execDailyCalendar();
			// Prepares the task.
			//			MyTask task = new MyTask();
			//			// Schedules the task, once every minute.
			//			String cron = "*/5 * * * * ?"; //once every 5 seconds.
			//			// Starts the scheduler.
			//			//scheduler.start();
			//			// Stays alive for five minutes.
			//			//
			//			Calendar calendar = Calendar.getInstance();
			//			calendar.set(2010, Calendar.DECEMBER, 25);
			//			HolidayCalendar hc = new HolidayCalendar();
			//			hc.addExcludedDate(calendar.getTime());
			//			// Creates the scheduler.
			//			Scheduler scheduler = new StdSchedulerFactory().getScheduler("DefaultScheduler");
			//			scheduler.addCalendar("calName", hc, true, false);
			//			//
			//			Trigger cronTrigger = CronScheduleBuilder.cronSchedule(cron).build();
			//			cronTrigger.setCalendarName("calName");
			//			//
			//			scheduler.schedule(new Work(), cronTrigger);
			//			//Thread.sleep(5L * 60L * 1000L);
		} catch (Exception e) {
			;
		}
		// Stops the scheduler.
		//scheduler.stop();
	}
}
